global root_dir = "`1'"

include "$root_dir/code/config/config.do"

cap noi log using ${log_dir}/figure_A5_skillpremium_trend_germany.log, replace name(figA)

capture noi {
    ***********************************************
    * Plot ISP trend (in levels) by Germany and ROW
    ***********************************************

    /* illustrate the evolution of the skill premium in Germany and the Rest of the World (ROW). 
        It descriptively supplements the Harz Reform exercise.*/

    **************************************
    * Load country list
    **************************************

    clear
    qui do ${code_dir}/config/country_list.do
    global countrylist_wo_de : subinstr global countrylist1995 "DE" "", word

    **********************************************
    * Run panel A poisson reg for extracting firms
    **********************************************

    qui do ${code_dir}/figures/figure_5a_hartz_eventstudy_a.do "$root_dir"
    gen in_use = 0
    replace in_use = 1 if e(sample)==1
    bys BvD: gen cum_in_use = sum(in_use)
    egen max_in_use = max(cum_in_use), by(BvD)
    gen in_sample_a = 1 if max_in_use > 0
    drop if in_sample_a ==.
    keep BvDIDnumber in_sample_a
    tempfile firmlist_a
    save `firmlist_a', replace 
    save ${tmp_dir}/firmlist_a.dta, replace 
    

    *************************************************
    * Build weighted wages and skill premium variable
    ************************************************

    * Merge firms with sample used in the reg for panel A
    use BvD year lswMPm_1995_a hswMPm_1995_a country_shr_1995 maxweight_1995 using ${final_dir}/regression_dataset_from1970_tfacit1.dta, clear 
    duplicates drop BvD year, force
    mmerge BvD using _tmp/firmlist_a, unmatched(master)

    keep if in_sample_a == 1

    * Merge with weights, mark Germany, filter
    mmerge BvD using ${dataset_dir}/weights/bvdid_pat_weights_EPtr_from1970_1995_orbis2017_tfacit1.dta, unmatched(master)
    duplicates drop BvD, force
    ren country_shr_1995 country
    ren maxweight_1995 max_weight
    gen target_country = "DE"
    keep BvDIDnumber max_weight country target_country lswMPm_1995_a hswMPm_1995_a share2_all_1995_*

    * Renormalize and calculate weights without Germany
    rename share2_all_1995_DE weight_de
    egen total_weight = rowtotal(share2_all_1995_*)
    foreach ctry of global countrylist_wo_de {
        gen noDEweight_`ctry' = share2_all_1995_`ctry' / total_weight
    }
    egen total_weight_woDE = rowtotal(noDEweight_*) // just as a a check if they sum up to 1

    * Calculate average noDEweight across firms for each country
    collapse noDEweight_*
    rename noDEweight_* theta_*

    * Add LS and HS wages
    cross using ${dataset_dir}/indep_vars/lswages_wide_MANUF.dta
    mmerge year using ${dataset_dir}/indep_vars/hswages_wide_MANUF.dta
    keep if year >= 1995 & year <= 2009
    keep year theta_* lswMP* hswMP* 

    * Calculate weighted average "rest of world" high and low skill wages & skill premium
    foreach ctry of global countrylist_wo_de {
        gen lswMP_wa_`ctry' = theta_`ctry' * lswMP_`ctry'
        gen hswMP_wa_`ctry' = theta_`ctry' * hswMP_`ctry'
    }
    egen lswMP_ROW = rowtotal(lswMP_wa_*)
    egen hswMP_ROW = rowtotal(hswMP_wa_*)  
    gen skillpremium_ROW_inv = lswMP_ROW / hswMP_ROW
    gen skillpremium_DE_inv = lswMP_DE / hswMP_DE
    keep year skillpremium_* lswMP_ROW hswMP_ROW lswMP_DE hswMP_DE 

    save ${tmp_dir}/de_row_data.dta, replace 


    **************************************
    * Plot Inverse Skill Premium Graph   *
    **************************************

  
    qui include ${code_dir}/config/figuretools.do

    grstyle set margin ".1 1.75 .1 .1", cm: graph
    
    * LOG WAGES

    gen loglswMP_DE = log(lswMP_DE)
    gen loghswMP_DE = log(hswMP_DE)
    gen loglswMP_ROW = log(lswMP_ROW)
    gen loghswMP_ROW = log(hswMP_ROW)

    colorpalette ptol, n(5) nograph
    twoway (scatteri 2.5 2003 4.3 2003 4.3 2005 2.5 2005, recast(area) bcolor(gs14%25) lwidth(0)) ///
        connected loglswMP_DE loghswMP_DE loglswMP_ROW loghswMP_ROW year, ///
        ylabel(, grid glpattern(dash) glwidth(thin) glcolor(gs14)) xtitle("Year") ///
        msymbol(o o d d) lpattern(solid solid solid solid) color("`crm3'" "`crm3'" gs6 gs6) ///
        ytitle("Log Wages") xlab(1996(4)2008, nogrid) plotregion(margin(b=0)) ylab(2.5(0.5)4) yscale(range(2.5 4.3)) ///
        text(2.75 2004 "{it:Hartz}" "{it:reforms}", just(center)) ///
        legend(off) ///
        || pcarrowi 2.6 2003 2.6 2005, recast(pcbarrow) lwidth(thin) mcolor(gs4) barbsize(0) lcolor(gs4) ///
        text(3.05 2009.3 "Low-skill" "Germany", color("`crm3'") place(e) just(left)) /// Low{superscript:DE}
        text(4.1 2009.3 "High-skill" "Germany", color("`crm3'") place(e) just(left)) /// w{subscript:H}
        text(3.2 2009.3 "Rest of" "World", color(gs6) place(e) just(left)) ///
        text(3.8 2009.3 "Rest of" "World", color(gs6) place(e) just(left)) ///
        title("(A)  Log wages", position(6)) name(lowhigh, replace)
    graph export ${fig_dir}/appendix/Figure_A5_logwages_trend_germany.pdf, as(pdf) replace
    graph export ${fig_dir}/appendix/Figure_A5_logwages_trend_germany.eps, as(eps) replace

    * ISP ROW and DE
    twoway scatteri 0.3 2003 0.56 2003 0.56 2005 0.3 2005, recast(area) bcolor(gs14%25) lwidth(0) || ///
        connected skillpremium_ROW_inv skillpremium_DE_inv year, ///
        yscale(range(0.3, 0.56)) ylabel(0.3 (0.1) 0.5, grid glpattern(dash) glwidth(thin) glcolor(gs14)) xtitle("Year") ///
        msymbol(d o) lpattern(solid solid) color(gs6 "`crm3'") ///
        ytitle("Inverse skill premium") xlab(1996(4)2008, nogrid) plotregion(margin(b=0)) legend(off) ///
        text(0.515 2009.3 "Rest of" "World", color(gs6) place(e) just(left)) text(0.35 2009.3 "Germany", color("`crm3'") place(e) just(left)) ///
        text(0.334 2004 "{it:Hartz}" "{it:reforms}", just(center)) ///
        || pcarrowi 0.314 2003 0.314 2005, recast(pcbarrow) lwidth(thin) mcolor(gs4) barbsize(0) lcolor(gs4) ///
        title("(B)  Inverse Skill premium", position(6)) name(isp, replace)

    grc1leg2 lowhigh isp, xsize(12) ysize(7) loff 
    graph export ${fig_dir}/appendix/Figure_A5_wagetrend_germany_row.pdf, as(pdf) replace
    graph export ${fig_dir}/appendix/Figure_A5_wagetrend_germany_row.eps, as(eps) replace



}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}

cap log close figA
